﻿using UnityEngine;
using System.Collections;

public partial class LightCitizenFSM
{
    //-----------------------------------IdleState------------------------------------------
    IEnumerator IdleState()
    {


        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.IDLE);
        gameObject.transform.GetChild(1).gameObject.SetActive(true);

        do
        {
            //구현

            yield return null;

            CoverEnable();
            CloseAtkEnable();
            AimEnable();

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.IDLE);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);

    }

    //-----------------------------------MoveState------------------------------------------
    IEnumerator MoveState()
    {
        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.MOVE);

        m_vecDest = GameObject.Find("ProtectTarget").transform.position;

        gameObject.GetComponent<AStar>().AStarStart(Calculate.GetMyTileIdx(transform.position), Calculate.GetMyTileIdx(m_vecDest));

        m_BestList = gameObject.GetComponent<AStar>().GetBestList();

        gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;
        gameObject.transform.GetChild(1).gameObject.SetActive(true);

        do
        {
            yield return null;

            CloseAtkEnable();
            //TraceEnable();
            //TauntEnable();
            AimEnable();

            //Astar

            BASEUNIT_STATE CurrentStat = AStarMove();
            if (CurrentStat != BASEUNIT_STATE.MOVE)
            {
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = CurrentStat;
            }

            //End  

            if (CommonStateChange())
                break;


        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.MOVE);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }

    IEnumerator DamageState()
    {
        float fCurrentTime = 0.0f;

        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.DAMAGE);
        //gameObject.GetComponent<PolyNavAgent>().Stop();
        gameObject.transform.GetChild(1).gameObject.SetActive(false);

        do
        {
            yield return null;

            fCurrentTime += Time.deltaTime;

            if (fCurrentTime >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length)
            {
                fCurrentTime = 0.0f;
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;
                break;
            }

            if (CommonStateChange())
                break;


        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.DAMAGE);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }

    IEnumerator DeadState()
    {
        float fCurrentTime = 0.0f;

        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.DEAD);
        //gameObject.GetComponent<PolyNavAgent>().Stop();
        gameObject.transform.GetChild(1).gameObject.SetActive(false);

        do
        {
            yield return null;

            fCurrentTime += Time.deltaTime;

            if (fCurrentTime >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length)
            {
                fCurrentTime = 0.0f;
                //시체로 변함
                Destroy(gameObject.GetComponent<CircleCollider2D>());
                //Destroy(gameObject.GetComponent<PolyNavAgent>());
                gameObject.GetComponent<SpriteRenderer>().sortingLayerName = "Props";
            }


        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.DEAD);

    }

    IEnumerator RetreatState()
    {
        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.RETREAT);
        m_vecDest = new Vector3(gameObject.transform.position.x, 10.0f, 0.0f);
        //gameObject.GetComponent<PolyNavAgent>().SetDestination(m_vecDest);
        gameObject.transform.GetChild(1).gameObject.SetActive(false);

        do
        {
            yield return null;

            if (CommonStateChange())
                break;

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.RETREAT);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);

    }
}
